package com.glorystone.ins.service.impl;

import com.glorystone.ins.dao.BusinessLogMapper;
import com.glorystone.ins.domain.BusinessLog;
import com.glorystone.ins.domain.SysUser;
import com.glorystone.ins.service.BusinessLogService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wangwei on 2018/3/15.
 */
@Service
public class BusinessLogServiceImpl implements BusinessLogService {

    @Autowired
    private BusinessLogMapper businessLogMapper;


    @Override
    public void save(BusinessLog.BusinessTypeEnum businessTypeEnum, Long businessId, BusinessLog.ContentEnum content, String remark) {

        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();

        BusinessLog log = BusinessLog.builder()
                .businessType(businessTypeEnum.getValue())
                .businessId(businessId)
                .content(content.getValue())
                .remark(remark).build();
                if (user == null) {
                    log.setCreateBy(1L);
                }else{
                    log.setCreateBy(user.getTid());
                }
        businessLogMapper.save(log);

    }

    @Override
    public List<BusinessLog> findList(BusinessLog.BusinessTypeEnum businessTypeEnum, Long businessId) {
        return findList(businessTypeEnum.getValue(), businessId);
    }

    @Override
    public List<BusinessLog> findList(Integer businessType, Long businessId) {
        BusinessLog log = BusinessLog.builder().businessType(businessType).businessId(businessId).build();
        return businessLogMapper.findList(log);
    }
}
